package edu.ustb.efws.base.controller;

import edu.ustb.efws.base.controller.form.PositionSaveForm;
import edu.ustb.efws.base.controller.form.PositionUpdateForm;
import edu.ustb.efws.base.domain.Position;
import edu.ustb.efws.base.service.PositionService;
import edu.ustb.efws.controller.vo.OkVo;
import edu.ustb.efws.controller.vo.ResultPageVo;
import edu.ustb.efws.controller.vo.ResultVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * 风险点设置API接口
 *
 * @author TS Group
 */
@RestController
@RequestMapping("/manage/position")
@Api(value = "/manage/position", tags = "风险点设置管理")
public class PositionController {
    private final PositionService service;

    @Autowired
    public PositionController(PositionService service) {
        this.service = service;
    }

    @PostMapping(consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation("新增风险点设置")
    public ResultVo<Position> save(@Valid @RequestBody PositionSaveForm form){
        return ResultVo.success(service.save(form.toDomain()));
    }

    @PutMapping(consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation("修改风险点设置")
    public ResultVo<Position> update(@Valid @RequestBody PositionUpdateForm form){
        return ResultVo.success(service.update(form.toDomain()));
    }

    @DeleteMapping(value = "{id}", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation("删除风险点设置")
    public ResultVo<OkVo> delete(@PathVariable("id")int id){
        return ResultVo.success(new OkVo(service.delete(id)));
    }

    @GetMapping(value = "{id}", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation("获取风险点设置")
    public ResultVo<Position> get(@PathVariable("id")int id){
        return ResultVo.success(service.get(id));
    }

    @GetMapping(produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation("查询风险点设置")
    public ResultPageVo<Position> query(
            @ApiParam(value = "风险值输出点编号") @RequestParam(required = false) String outNumber,
            @ApiParam(value = "风险值输出位置") @RequestParam(required = false) String area,
            @ApiParam(value = "风险值输出点位置显示") @RequestParam(required = false) String position,
            @RequestParam(defaultValue = "true") @ApiParam(value = "是否查询记录数") Boolean isCount,
            @RequestParam(defaultValue = "0") @ApiParam(value = "查询页数") int page,
            @RequestParam(defaultValue = "15") @ApiParam(value = "查询每页记录数") int rows){

        return new ResultPageVo
                .Builder<>(page, rows, service.query(outNumber, area, position, page * rows, rows))
                .count(isCount, () -> service.count(outNumber, area, position))
                .build();
    }
}
